<template>
  <div class="app-container">
    <h4>基本信息</h4>
    <div class="change">
      <p>
        <span>退库单号</span>
        <el-input v-model="tempData.recipientsNumber" :disabled="islook" type="text" class="userselect" placeholder="请输入退库单号" />
      </p>
      <p>
        <span>退库处理人</span>
        <el-input placeholder="退库处理人" v-model="tempData.managePerson" disabled class="userselect"></el-input>
      </p>
      <p>
        <span>退库部门</span>
        <el-input placeholder="退库部门" v-model="tempData.receiveDepartment" disabled class="userselect"></el-input>
      </p>
      <p>
        <span>退库时间</span>
        <el-date-picker
          v-model="tempData.receiveTime"
          type="date"
          value-format="yyyy-MM-dd"
          class="userselect"
          :disabled="islook"
          placeholder="选择日期"
        ></el-date-picker>
      </p>

      <p>
        <span>退库后使用公司</span>
        <el-select v-model="tempData.belongCompany" :disabled="islook" placeholder="请选择退库后使用公司" class="userselect">
          <el-option
            v-for="item in companyList"
            :key="item.sid"
            :label="item.names"
            :value="item.names"
          ></el-option>
        </el-select>
      </p>

      <p>
        <span>退库后存放区域</span>
        <el-select v-model="tempData.receiveArea" :disabled="islook" placeholder="请选择退库后存放区域" class="userselect">
          <el-option v-for="item in diqu" :key="item.sid" :label="item.names" :value="item.names"></el-option>
        </el-select>
      </p>

      <p>
        <span>退库后存放地</span>
        <el-select v-model="tempData.receivePlace" :disabled="islook" placeholder="请选择退库后存放地" class="userselect">
          <el-option
            v-for="item in cunfangdi"
            :key="item.sid"
            :label="item.names"
            :value="item.names"
          ></el-option>
        </el-select>
      </p>

      <p>
        <span>退库说明</span>
        <el-input v-model="tempData.receiveRemarks" :disabled="islook" type="text" class="userselect" placeholder="请输入备注" />
      </p>
    </div>

    <h4>选择资产</h4>
    <div class="zd3-btm">
      <div class="addDate" @click="addzichan" v-if="!islook">+ 选择资产</div>
      <el-table :data="proData" :show-header="true" style="width: 100%;border-radius: 8px;">
        <el-table-column type="index" align="center" width="50" label="序号" />
        <el-table-column align="center" prop="propertySn" width="150" label="资产SN编码" />
        <el-table-column align="center" prop="propertyName" width="150" label="资产名称" />
        <el-table-column align="center" prop="propertyType" width="150" label="资产类别" />
        <el-table-column align="center" prop="companys" width="150" label="当前所在公司" />
        <el-table-column align="center" prop="departments" width="150" label="当前所在部门" />
        <el-table-column align="center" prop="usePerson" width="150" label="当前使用人" />
        <el-table-column align="center" prop="storeAreas" label="存放地点" />
        <el-table-column align="center" width="80" label="操作" fixed="right" v-if="!islook">
          <template slot-scope="scope">
            <el-popconfirm
              placement="top"
              hideIcon
              title="是否要删除此行？"
              @onConfirm="deleteRow(scope.row,scope.$index)"
            >
              <el-button type="text" size="small" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="submits" v-if="!islook">
      <div class="save" @click="saveOrder">确认退库</div>
      <div class="nosave" @click="nosaveOrder">取消</div>
    </div>
    <!-- 报修人弹窗 -->
    <el-dialog title="选择报修人" :visible.sync="buyerShow" width="80%" top="20px">
      <div class="files">
				<div class="files-left">
					<el-menu class="el-menu-vertical-demo" @select="handleSelece" default-active="0">
						<el-menu-item :index='index' v-for="(item,index) in ifydata" :key="index">
							<span slot="title">{{item.departments}}</span>
						</el-menu-item>
					</el-menu>
				</div>
				<div class="files-right">
					<div class="files-btm">
						<el-table 
              :data="peopleData" 
              height="400" 
              highlight-current-row
              @current-change="handlePeople"
              style="margin-bottom: 20px;" 
              :show-header="true" >
							<el-table-column align="center" prop="username" label="姓名"></el-table-column>
							<el-table-column align="center" prop="departments" label="所属部门"></el-table-column>
							<el-table-column align="center" prop="sex" label="性别"></el-table-column>
							<el-table-column align="center" prop="power" label="职务"></el-table-column>
						</el-table>
						<el-pagination background hide-on-single-page :page-size="50" layout="prev, pager, next" :total="total" @current-change='goPage' @prev-click="goPrev" @next-click="goNext" />
					</div>
				</div>
			</div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="buyerShow = false">取 消</el-button>
        <el-button size="small" type="primary" @click="buyerShow = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="选择资产" :visible.sync="addProShow" width="85%" top="20px" :close-on-click-modal="false">
      <div class="product">
        <div class="pro-l">
          <el-tree
            :data="twoClassify"
            node-key="name"
            highlight-current
            :props="{label: 'name',children:'depositoryDtoList'}"
            @current-change="handleNodeClick"
          ></el-tree>
        </div>
        <div class="pro-r">
          <div class="zd3-title">
            <p>
              <span>资产名</span>
              <input v-model="propertyName" type="text" placeholder="请输入资产名" />
            </p>
            <el-button size="small" type="success" @click="searchList">查询</el-button>
            <el-button size="small" @click="resetList">重置</el-button>
          </div>
          <el-table
            ref="singleTable"
            :data="proDeail"
            border
            highlight-current-row
            v-loading="assetload"
            @current-change="handleAdd"
            style="width: 100%"
            height="550"
          >
            <el-table-column align="center" label="照片">
              <template slot-scope="scope">
                <img :src="scope.row.imgurl" alt="" class="avatar">
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" width="100" label="资产状态" fixed="left">
              <template slot-scope="scope">
                <div class="orderStatus">
                  <p v-if="scope.row.status === '闲置'" class="yellow">闲置</p>
                  <p v-if="scope.row.status === '报废'" class="red">报废</p>
                  <p v-if="scope.row.status === '借用'" class="blue">借用</p>
                  <p v-if="scope.row.status === '使用中'" class="green">使用中</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="propertySn" width="120" label="资产SN码"/>
            <el-table-column align="center" prop="propertyName" label="资产名称"/>
            <el-table-column align="center" prop="propertyType" label="资产类别"/>
            <el-table-column align="center" prop="propertyGuige" label="资产规格"/>
            <el-table-column align="center" prop="unit" label="计量单位" />
            <el-table-column align="center" prop="propertyJine" label="金额" />
            <el-table-column align="center" prop="propertyNums" label="数量" />
            <el-table-column align="center" prop="companys" label="使用公司" />
            <el-table-column align="center" prop="departments" label="使用部门" />
            <el-table-column align="center" prop="usePerson" label="使用人" />
            <el-table-column align="center" prop="areas" label="区域名称" />
            <el-table-column align="center" prop="storeAreas" label="存放地点" />
            <el-table-column align="center" prop="managerPerson" label="管理员" />
            <el-table-column align="center" prop="belongCompany" label="所属公司" />
            <el-table-column align="center" prop="purchaseTime" label="购入日期" />
            <el-table-column align="center" prop="suppliers" label="供应商" />
            <el-table-column align="center" prop="sourceOf" label="来源" />
          </el-table>
          <el-pagination
            background
            align="right"
            layout="prev, pager, next"
            :page-count="totalAssets"
            @current-change='goPageAssets'
            @prev-click="goPrevAssets"
            @next-click="goNextAssets"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUser, getBumenNew, getUserFiles } from "@/api/people";
import { getGongying,addLingTUi,updateLingTUi,rukuList,getFenlei,getSeting } from "@/api/caigou";
let selectid = null,navindex = null;
export default {
  data() {
    return {
      radio: "1",
      total: 1,
      page: 1,
      totalAssets: 1,
      pageAssets: 1,
      value: "",
      search: {
        propertyName: "",
        companys: "",
        currentPage: 1,
      },
      tableData: [],
      proData: [],
      oneClassify: [], //一级分类
      twoClassify: [],
      tempData: {
        receiveArea: "", //领用后存放区域
        receiveCompany: "", //领用后使用公司
        receiveRemarks: "", //领用说明
        receivePerson: "", //领用人
        receiveDepartment: "", //领用后使用部门
        receiveTime: "", // 领用时间
        recipientsNumber: "", //领用单号
        managePerson: "", //领用处理人
      },
      cunfangdi: [],
      diqu: [],
      companyList: [],
      fileNum: [
        {
          filename: "",
          filePath: "",
        },
      ], //需要上传的文件个数
      ifydata: [], //部门列表
      bumen: "", //部门值
      gongying: [], //供应商管理
      gys: "", //供应商选择值
      proDeail: [], //商品列表
      peopleData: [], //人员列表
      currentRow: null,
      gyShow: false, //供应商弹窗状态
      buyerShow: false, //采购员弹窗状态
      addProShow: false, //商品信息弹窗
      proData: [],
      propertyName: '', //根据名称搜索设备
      writOradd: 0, //0新增，1编辑
      islook: false,
      assetload: false
    };
  },
  mounted() {
    this.tempData.receiveDepartment = this.$store.state.user.userInfo[0].departments
    if(sessionStorage.getItem('bumentui')){
      this.tempData = JSON.parse(sessionStorage.getItem('bumentui'))
      console.trace('tempData:',this.tempData)
      this.proData = this.tempData.putList
      this.writOradd = 1
    }else{
      this.writOradd = 0
    }
		if(this.$route.params.look){
			this.islook = true
			console.log(this.islook)
		}else{
      const userinfo = this.$store.state.user.userInfo[0]
      this.tempData.managePerson = userinfo.username
      this.getpeople(userinfo.departments)
      this.bumenList();
      this.getSet();
      this.getAsset();
      this.gongyingList();
      this.getClassify()
      console.log(this.$route.params);
    }
  },
  methods: {
    /**
     * 保存领用单
     */
    saveOrder() {
      if(!this.proData.length){
        console.log('meiyouxuanze')
        this.$message({
          message: '请选择资产！',
          type: 'warning'
        })
        return
      }
      console.log(this.proData)
      console.log(this.tempData)
      this.tempData.status = '退库'
      this.tempData.ptype = '未确认'
      this.proData.map(item => {
        delete item.propertyType
        item.usePerson = ''
        item.departments = ''
        item.companys = this.tempData.receiveCompany
        item.areas = this.tempData.belongEreas
        item.storeAreas = this.tempData.belongPlace
        item.status = '闲置'
        item.ptype = '未确认'
        return item
      })
      this.tempData.putList = this.proData
      addLingTUi(this.tempData).then(res => {
        console.log('新增领用',res)
        this.$message({
          message: res.data.message,
          type: 'success'
        })
        this.$router.go(-1)
      })
    },
    // 选择退库处理人
    checkBuyer() {
      this.buyerShow = true;
    },
     /**
     * 根据资产名搜索
     */
    searchList(){
      this.assetload = true
      this.getAsset()
    },
    /**
     * 重置名称搜索
     */
    resetList(){
      this.propertyName = null
    },
    /**
     * 获取资产列表
     */
    getAsset(type) {
      rukuList({
        parentId: type ? type : null,
        departments: this.$store.state.user.userInfo[0].departments,
        propertyName: this.propertyName ? this.propertyName : null,
        currentPage: this.pageAssets
      }).then((res) => {
        console.log('资产',res);
        let temparr = res.data.list.filter(item => item.status === '使用中')
        this.totalAssets = res.data.pages
        this.proDeail = temparr
        this.assetload = false
      });
    },
    getSet(){
      getSeting({}).then(res => {
        console.log(res)
        // 存放仓库
        this.cunfangdi = res.data.filter(item => item.status === '3')
        // 存放地区
        this.diqu = res.data.filter(item => item.status === '2')
        //公司列表
        this.companyList = res.data.filter(item => item.status === '1')
      })
    },
    // 人员列表
    getpeople(id) {
      console.log("id:", id);
      getUser({ departments: id,currentPage: this.page }).then((res) => {
        console.log(res);
        this.peopleData = res.data.list;
        this.total = res.data.total
      });
    },
    // 部门列表
    bumenList() {
      getBumenNew({}).then((res) => {
        console.log("----------------");
        console.log(res);
        let temp = res.data.filter(item => item.ids === '456')[0].departmentsList
        console.log(temp)
        temp.map((item) => {
          item.value = item.departments;
          return item;
        });
        this.ifydata = temp;
      });
    },
    /**
     * 获取资产分类
     */
    getClassify(){
      getFenlei({}).then(res => {
        console.log('资产分类',res)
        this.twoClassify = res.data
      })
    },
    // 供应商列表
    gongyingList() {
      getGongying({}).then((res) => {
        console.log(res);
        this.gongying = res.data;
      });
    },
    // 选择人员分类
    handleSelece(index) {
      console.log(index);
      console.log("ifydata:", this.ifydata[index]);
      navindex = this.ifydata[index].value
        ? this.ifydata[index].value
        : null;
      this.getpeople(navindex);
    },
    // 资产分页
    goPrevAssets(page) {
      console.log(page)
      this.pageAssets--
      this.getAsset(selectid)
    },
    goNextAssets(page) {
      console.log(page)
      this.pageAssets++
      this.getAsset(selectid)
    },
    goPageAssets(page){
      console.log(page)
      this.pageAssets = page
      this.getAsset(selectid)
    },
    // 人员分页
    // 分页
    goPrev(page) {
      console.log(page)
      this.page--
      this.getpeople(navindex)
    },
    goNext(page) {
      console.log(page)
      this.page++
      this.getpeople(navindex)
    },
    goPage(page){
      console.log(page)
      this.page = page
      this.getpeople(navindex)
    },
    addzichan() {
      this.addProShow = true;
    },
    // 点击分类菜单获取对应商品
    handleNodeClick(data, checked, node) {
      console.log(data);
      selectid = data.id
      this.assetload = true
      this.getAsset(data.id)
    },
    goPrev(page) {
      console.log(page)
      this.page--
      this.getAsset(selectid)
    },
    goNext(page) {
      console.log(page)
      this.page++
      this.getAsset(selectid)
    },
    goPage(page){
      console.log(page)
      this.page = page
      this.getAsset(selectid)
    },

    // 选择操作人员
    handlePeople(row) {
      console.log(row);
      this.tempData.operator = row.username;
      console.log(this.tempData.buyer);
      this.buyerShow = false;
    },

    //将选中的资产追加到proData数组中
    handleAdd(row) {
      console.log(row);
      row.ischeck = false;
      row.num = 1;
      let index = this.proData.findIndex(item => item.oid === row.oid)
      if(index == -1){
        this.proData.push({...row})
        console.log(this.proData)
        this.addProShow = false
      }else{
        this.$message({
          message: '该资产已选择！',
          type: 'warning'
        })
      }
    },

    // 删除对应商品数据
    deleteRow(value, index) {
      console.log(index);
      this.proData.splice(index, 1);
    },
    // 取消新增
    nosaveOrder() {
      console.log("取消");
      this.$router.go(-1);
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/.el-input-group__append button.el-button{
  padding: 8px 12px;
}
.orderStatus {
  display: flex;
	justify-content: center;
  > p {
    padding: 0px 5px;
    color: #fff;
    font-size: 12px;
    border-radius: 2px;
    margin: 0 0px 0 0;
  }
  .blue {
    background: #3a87ad;
  }
  .green {
    background: #18a05e;
  }
  .yellow {
    background: #ff7a0e;
  }
  .red {
    background: #c60000;
  }
  .zise{
    background: #c7ab10
  }
}
.popup {
  padding: 0 20px;
}
.btns {
  display: flex;
  align-items: center;
  margin-left: 10px;
  /*position: absolute;
	right: 20px;
	bottom: 5px;*/
  > h3 {
    width: 64px;
    height: 32px;
    background: #18a05e;
    border-radius: 3px;
    color: #fff;
    font-size: 14px;
    line-height: 32px;
    font-weight: 100;
    text-align: center;
    cursor: pointer;
  }
}
.product{
  display: flex;
  .pro-l{
    width: 200px;
  }
  .pro-r{
    flex: 1;
    overflow: hidden;
    /deep/.el-table--enable-row-hover .el-table__body tr:hover>td {
      background-color: rgba(24,160,94,.2);
    }
    /deep/.el-table tr{
      cursor: pointer;
    }
    /deep/.el-table__body tr.current-row>td {
      background-color: rgba(24,160,94,.4);
    }
  }
}
/deep/.el-dialog__body {
  padding: 10px 20px;
}
.zd3-title {
	height: 60px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	// border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 20px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 150px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}
}
.files {
  display: flex;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  margin-top: 20px;
  overflow: hidden;
  padding: 20px;
}
/deep/.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #18a05e;
  color: #fff;
}
.files-btm /deep/.btn-prev:after {
  content: "上一页";
}
.files-btm /deep/.btn-next:before {
  content: "下一页";
}
.files-btm /deep/.btn-prev,
.files-btm /deep/.btn-next {
  width: 65px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}
.files-left {
  width: 160px;
  margin-right: 20px;
  /deep/.el-menu {
    border-right: none;
  }
  /deep/.el-menu-item {
    height: 35px;
    line-height: 35px;
    margin-bottom: 8px;
  }
  /deep/.el-menu-item.is-active {
    color: #fff;
    border-radius: 5px;
    background: rgba(24, 160, 94, 0.8);
  }
}
.files-right {
  flex: 1;
  overflow: hidden;
}
.files-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 45px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.avatar {
  width: 30px;
  height: 30px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 0 0 8px 8px;
  // border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.inps {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.inps-active {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: rgba(24, 160, 94, 0.2);
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
h4 {
  background: #fff;
  padding-top: 12px;
  padding-bottom: 12px;
  margin: 0;
  padding-left: 10px;
  border-radius: 8px 8px 0 0;
  border-bottom: 1px solid #ccc;
}
.change {
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 0 0 8px 8px;
  padding: 20px;
  margin-bottom: 20px;
  > p {
    width: 25%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
      display: flex;
      line-height: 30px;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
.submits {
  display: flex;
  padding: 15px 20px;
  justify-content: flex-end;
  align-items: center;
  > div {
    width: 80px;
    height: 32px;
    text-align: center;
    line-height: 32px;
    color: #fff;
    font-size: 14px;
    border-radius: 4px;
    margin-left: 30px;
    cursor: pointer;
  }
  .save {
    background: #2196f3;
  }
  .nosave {
    background: #999;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #999 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px solid #ccc;
}

.fileup {
  padding: 20px 0;
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 0 0 8px 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  // justify-content: space-between;
  // align-items: center;
  flex-direction: column;
  // justify-content: center;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    /deep/.el-upload-dragger {
      width: 100%;
      height: 100px;
    }
    /deep/.el-upload-dragger .el-icon-upload {
      font-size: 60px;
      color: #c0c4cc;
      margin: 0;
      line-height: 1;
      width: 100%;
    }
    /deep/.el-upload {
      width: 100%;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: inline-block;
      width: 100px;
      text-align: right;
      padding-right: 15px;
      &:last-child {
        text-align: left;
      }
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
    > strong {
      display: inline-block;
      padding-right: 100px;
      font-weight: 100;
      font-size: 14px;
      &:last-child {
        cursor: pointer;
        color: #13ce66;
        font-weight: 600;
      }
    }
    > div {
      width: inherit;
      flex: 1;
    }
  }
}
@media screen and (max-width: 1400px) {
  .change > p {
    width: 33%;
  }
}
</style>